Release 10.1A: OpenEdge Data Management:
DataServer for ORACLE
Loading a result set into a temp-table
Enhancements implemented through changes to the
RUN STORED-PROCstatement allow you to retrieve a result set from a foreign data source and load the result set, for which a temp-table handle is defined, into its own temp-table. TheLOAD-RESULT-INTOfunction enables data retrieved to be loaded into temp-tables where the data can then be manipulated, employing all characteristics inherent to temp-tables. The capability to load result sets into temp-tables is not limited by the parsing requirements associated with theproc-text-buffernor the database dependencies associated with views.Temp-tables can provide data management capabilities associated with the 4GL directly to the result sets of a stored procedure, but completely independent of the foreign data source from which it was populated and/or derived. Temporary tables are effectively database tables in which Progress stores data temporarily. Because temp-tables have the same support features that actual OpenEdge databases use, you can take advantage of almost all the OpenEdge database features that do not require data persistence and multi-user access. For example, you can define indexes for fields in the temp-table. For more information about temp-tables, see OpenEdge Development: Progress 4GL Handbook .
Example 3–9 introduces how to use the
RUN STORED-PROCstatement with theLOAD-RESULT-INTOphrase with a single dynamic temp-table. It highlights the coding techniques discussed in the "Run Stored-Proc statement with send-sql-statement option" section and introduces the dynamic temp-table topic further discussed in this section.
Example 3–9: Using the RUN STORED-PROC statement with LOAD-RESULT-INTO phrase with a a single dynamic temp-tableGetting started
If you are using static temp-tables, you must define the temp-table layout in your program to accommodate a specific stored procedure result set before attempting to populate these tables. Once this prerequisite task is done, however, temp-tables can also be automatically populated, offering a potential performance gain in most instances.
Unlike the proc-text-buffer technique, you do not have to parse the strings from the proc-text-buffer pseudo table buffer where each row is a character string. Similarly, you do not need to perform any administration to maintain views in the foreign data source or their definitions in the schema holder. For more details about planning your temp-table layout, see the "Creating a temp-table layout plan" section.
Employing additional enhancements
The temp-table technique offers even greater programming benefits as it extends both send-sql-statement options and stored procedures through the result processing techniques previously described in this chapter. For example, by mapping the
PROGRESS_RECID to the ROWIDfield in temp- tables, you can easily support KEY definitions required by the Progress DataSet (commonly referred to as a ProDataSets) to ensure that your data mappings between the foreign data source and the temp-table are accurate. Accurate data mappings are essential for sending data back from the temp-table to the data source. Non-ROWIDkey definitions can also be described with a unique key.ProDataSets functionality is based on one or more temp-tables that share and extend basic temp-table functionality. For more information about ROWID field and using the send-sql-statement with the
LOAD-RESULT-INTOoption, see the "ROWID support" section. For in depth discussion of temp-tables and more information about ProDataSets, see OpenEdge Development: ProDataSets .Table 3–3 highlights additional language elements you can use with the stored procedure and the send-sql statement language to use
ROWID.
Creating a temp-table layout plan
You must define the temp-table layout in your application program to accommodate specific result sets before you attempt to populate the temp-tables with data. If a SQL statement retrieves more than one result set, you must define multiple temp-tables to be able to retrieve all the data. Therefore, the success of this approach depends to a large extent on your:
The following types of temp-tables can support result sets:
Keep in mind that you can pass handles of temp-tables that contain a mixed array. A mixed array is one in which some of the temp-table handle elements can be static while others can be dynamic.
Table 3–4 identifies the temp-table options for which you can plan and the requirements you must fulfill for each option.
Using a temp-table handle with an unprepared dynamic temp-table
When a temp-table handle points to an unprepared dynamic temp-table, the ORACLE Server DataServer defines the temp-table schema in the form of the result sets record structure which is passed back to the DataServer from the foreign data source. The data types defined for the temp-table schema are determined based on the default data type mapping that exists between the SQL data type and its equivalent Progress default data type. Once the temp-table schema is dynamically established by the DataServer, the result set begins to populate it.
Recognize that there is the possibility of a small performance price to be paid when you build dynamic temp-tables. However, considering the database independence that this technique affords over building static temp-tables, you might consider the price of dynamically built temp-tables to be a small, reasonable one.
Table 3–4: Options to plan the temp-table layout for result sets To return a result set to this type of temp-table... Then the layout definition is... StaticDynamic-prepared state Defined by you; you must base the layout on the expected fields to be returned and each of these fields’ data types so that the first field defined in the temp-table corresponds to the first column of the result set. This column matching and data type matching must be repeated successfully for each temp-table and its corresponding result set. Dynamic - unprepared state Not defined by you; the schema of the temp-table is based on the result-set schema and a mapping of default Progress data types for each SQL type. For more information, see the "Details about a Dynamic temp-table in an unprepared state" section.Note: Once the data is loaded into the temp-table, any updates made to the records in the temp-table are not propagated back to the foreign database. Result sets are available through temp-tables for the purpose of obtaining a snapshot of the data. For example, you can use this technique to populate a browser from a temp-table. You must re-read the record using the proper lock mechanism to actually update the record.
Details about a Dynamic temp-table in an unprepared state
A dynamic temp-table is considered to be in an unprepared state after the first definitional method is called until the temp-table is prepared. If a clear dynamic temp-table handle is passed, the DataServer populates the temp-table schema based on the result-set schema and prepares the temp-table. A clear dynamic temp-table is a table that is in an unprepared state where definitional methods have not yet been called. The DataServer then executes the temp-table handle:
ADD-NEW-FIELD(field name, data type) internally for each one of the columns of the result set. If a column in the result set from the foreign schema does not have a name (for example, an array element field or a SQL-derived field), the DataServer assigns a name to it based on the column position on the row.For example, if you run the following statement, then the temp-table contains columns:
column 1, cust_num, name and column4:
The data type associated with each column follows the mapping rules that exist between Progress and the foreign data source’s data types. For more information about data types and default mapping, see Chapter 2, " Initial Programming Considerations."
Note: Since a stored procedure can return multiple result sets, the DataServer prepares the temp-table as “result<n>” where <n> is the result-set sequence number that corresponds to its element position in the temp table handle array, starting with 1. Therefore, if there are 3 result sets and 3 clear dynamic temp-tables are passed, the temp-tables are called result1, result2, and result3.Note the following error conditions as they specifically apply to a dynamic temp-table:
- If an error occurs during the schema population or during the prepare of a temp-table, the DataServer raises an error condition.
- The dynamic temp-table must be either already prepared or clear with no defined fields in it. If fields have already been added to a dynamic temp-table before the
RUN STORED-PROCstatement is executed and the temp-table is not prepared, the DataSever raises an error condition due to the invalid state of the temp-table.Details about a Dynamic temp-table in a prepared state
Example 3–10 shows multiple dynamic temp-tables in a prepared state. It is based on the stored procedure code presented in Example 3–8 which shows the basics of executing a call to a stored procedure that returns multiple result sets using the functionality that the LOAD-RESULT-INTO phrase supports. Each result set will be loaded into a separate temp-table.
Example 3–10: Multiple dynamic temp-tables shown in a prepared stateAdditional temp-table examples
This section presents more examples that show various techniques to code temp-tables.
Example 3–11 shows the basics of executing a call to a stored procedure using the functionality that the
LOAD-RESULT-INTOphrase supports. Note that the code works with the stored procedure in Example 3–8 comparable to the way you can see it works with the 4GL code.
Example 3–11: Basic technique to execute a stored procedure call using the LOAD-RESULT-INTO phraseExample 3–12 shows the basics of using an existing dynamic temp-table without the
TEMP-TABLE-PREPARE()method. In this instance, the send-sql-statement option is used rather than a predefined stored proc. In contrast, the third example code that appears later in this section shows the same approach, but explicitly defines the existing dynamic temp-table with theTEMP-TABLE-PREPARE()method.
Example 3–12: Using an existing temp-table without the TEMP-TABLE-PREPARE ( ) methodExample 3–13 shows the basics of using an existing dynamic temp-table
with theTEMP-TABLE-PREPARE() method.
Example 3–13: Using an existing temp-table with the TEMP-TABLE-PREPARE ( ) methodNote these points as they relate the Example 3–13:
- As a prerequisite for creating the code shown in the previous example, the developer would need to define the schema for the table.
- Once the temp-table schema begins preparation from the clear state, the temp-table must be defined to the exact specifications for the result sets as generated by the
RUN STORED-PROCstatement or send-sql-statement option. Otherwise, theRUN STORED-PROCwill end in failure. Also, note theTEMP-TABLE-PREPAREmust be called at the completion of the temp-table definition associated with the stored procedure results.Example 3–14 shows the syntax for the stored procedure with the
LOAD-RESULT-INTOphrase with a single static temp-table and the send-sql-statement option.
Example 3–14: Calling a stored procedure that uses the LOAD-RESULT-INTO phrase with a single temp table and the send-sql-statement optionExample 3–15 shows the use of a
PROC-STATUSphrase. ThePROC-STATUSphrase must be defined as part of theRUN STORED-PROCstatement because of the implicitCLOSE STORED-PROCthat is associated with theLOAD-RESULT-INTOphrase.
Example 3–15: Using the PROC-STATUS phraseIn Example 3–15, note that the
PROC-STATUSphrase does not need aPROC-HANDLEphrase because it is retrieved using theRUN STORED-PROCstatement and not after this statement’s execution as it typically is used.Example 3–16 is a two-part example that shows the basics of executing a call to a stored function that returns a result set using a cursor and the functionality that the
LOAD-RESULT-INTOphrase supports. The result set will be loaded into a temp-table.
Example 3–16: Executing a call to a stored function that returns a result set using the LOAD-RESULT-INTO phrase.
|
Copyright © 2005 Progress Software Corporation www.progress.com Voice: (781) 280-4000 Fax: (781) 280-4095 |